package com.quaidi.smartlocker.controller;

import com.quaidi.smartlocker.annotation.RequireRole;
import com.quaidi.smartlocker.common.Result;
import com.quaidi.smartlocker.entity.SysAdmin;
import com.quaidi.smartlocker.enums.UserRole;
import com.quaidi.smartlocker.service.SysAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统管理员Controller
 * 
 * @author QuaiDi
 * @since 2025-08-26
 */
@RestController
@RequestMapping("/api/sys-admin")
public class SysAdminController {

    @Autowired
    private SysAdminService sysAdminService;

    /**
     * 获取所有管理员列表
     */
    @RequireRole(UserRole.ADMIN)
    @GetMapping("/list")
    public Result<List<SysAdmin>> getAdminList() {
        List<SysAdmin> adminList = sysAdminService.list();
        // 隐藏密码信息
        adminList.forEach(admin -> {
            admin.setPassword("******");
            admin.setSalt(null);
        });
        return Result.success(adminList);
    }

    /**
     * 根据ID获取管理员信息
     */
    @RequireRole(UserRole.ADMIN)
    @GetMapping("/{id}")
    public Result<SysAdmin> getAdminById(@PathVariable Long id) {
        SysAdmin admin = sysAdminService.getById(id);
        if (admin == null) {
            return Result.error("管理员不存在");
        }
        // 隐藏密码信息
        admin.setPassword("******");
        admin.setSalt(null);
        return Result.success(admin);
    }

    /**
     * 修改密码
     */
    @RequireRole(UserRole.ADMIN)
    @PutMapping("/change-password")
    public Result<String> changePassword(@RequestParam String username, @RequestParam String newPassword) {
        try {
            boolean success = sysAdminService.changePassword(username, newPassword);
            if (success) {
                return Result.success("密码修改成功");
            } else {
                return Result.error("密码修改失败");
            }
        } catch (Exception e) {
            return Result.error("密码修改失败：" + e.getMessage());
        }
    }

    /**
     * 创建新管理员
     */
    @RequireRole(UserRole.ADMIN)
    @PostMapping
    public Result<String> createAdmin(@RequestBody SysAdmin admin) {
        try {
            boolean success = sysAdminService.createAdmin(admin);
            if (success) {
                return Result.success("管理员创建成功");
            } else {
                return Result.error("管理员创建失败");
            }
        } catch (Exception e) {
            return Result.error("管理员创建失败：" + e.getMessage());
        }
    }

    /**
     * 启用/禁用管理员
     */
    @RequireRole(UserRole.ADMIN)
    @PutMapping("/{id}/status")
    public Result<String> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean success = sysAdminService.updateStatus(id, status);
            if (success) {
                return Result.success("状态更新成功");
            } else {
                return Result.error("状态更新失败");
            }
        } catch (Exception e) {
            return Result.error("状态更新失败：" + e.getMessage());
        }
    }
}